import 'package:gold/base/view_model.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/list_model.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 列表ViewModel基类
/// @author tangzhentao
/// @created at 11:12 2025/1/1


class ListViewModel<T> extends Viewmodel {
  /// 刷新控制器
  final RefreshController controller = RefreshController();

  /// 页码
  int _page = 1;

  /// 列表数据
  final List<T> list = [];

  /// 请求接口
  final String api;

  /// 更多数据判断使用lastPage
  bool isLast = true;

  ListViewModel(this.api);

  /// 首次加载
  Future firstLoad() async {
    hideKeyboard();
    setLoading();
    _page = 1;
    final result = await Http().request<ListModel<T>>(api, data: _refreshData());
    if (result.success) {
      list.clear();
      list.addAll(result.data?.list?? []);
      if (list.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.idle;
        controller.footerMode?.value = _page < (isLast ? (result.data?.lastPage?? 0):(result.data?.totalPages?? 0)) ? LoadStatus.idle:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 重新加载
  Future retryLoad() async {
    hideKeyboard();
    _page = 1;
    final result = await Http().request<ListModel<T>>(api, data: _refreshData(), loadingText: '加载中...');
    if (result.success) {
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
      }
      setSuccessChange();
      controller.headerMode?.value = RefreshStatus.idle;
      controller.footerMode?.value = _page < (isLast ? (result.data?.lastPage?? 0):(result.data?.totalPages?? 0)) ? LoadStatus.idle:LoadStatus.noMore;
    }
  }

  /// 下拉刷新
  Future refresh() async {
    _page = 1;
    final result = await Http().request<ListModel<T>>(api, data: _refreshData());
    if (result.success) {
      controller.refreshCompleted();
      controller.headerMode?.value = RefreshStatus.idle;
      controller.footerMode?.value = _page < (isLast ? (result.data?.lastPage?? 0):(result.data?.totalPages?? 0)) ? LoadStatus.idle:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 上拉加载更多
  Future loadMore() async {
    _page++;
    final result = await Http().request<ListModel<T>>(api, data: _refreshData());
    if (result.success) {
      if (_page < (isLast ? (result.data?.lastPage?? 0):(result.data?.totalPages?? 0))) {
        controller.loadComplete();
      } else {
        controller.loadNoData();
      }
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.loadFailed();
    }
  }

  /// 获取最新参数
  Map<String, dynamic> _refreshData() {
    final Map<String, dynamic> data = {'page': _page};
    final other = onOtherData();
    if (other != null) data.addAll(other);
    return data;
  }

  /// 额外参数
  Map<String, dynamic>? onOtherData() { return null; }

  @override
  onReload() => firstLoad();

  @override
  onRetry() => firstLoad();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}